#include "threadPool.h"
#include"utils.h"
#define clientclose 1
void *threadFunc(void * arg){
    threadPool_t * pthreadPool = (threadPool_t *)arg;
    while(1){
        // 取出任务
        pthread_mutex_lock(&pthreadPool->mutex);
        while(pthreadPool->taskQueue.queueSize == 0){
            printf("newWorker\n");
            pthread_cond_wait(&pthreadPool->cond,&pthreadPool->mutex);
        }
        if(pthreadPool->exitflag==1){
            printf("worker open mutex and exit\n");
            pthread_mutex_unlock(&pthreadPool->mutex);
            pthread_exit(NULL);
        }
        printf("worker got a task!\n");
        int netfd = pthreadPool->taskQueue.pfront->fd;
        Dequeue(&pthreadPool->taskQueue);
        pthread_mutex_unlock(&pthreadPool->mutex);
        //transFile(netfd);


    //recv 阻塞
        train_t train;
        char commandSave[1024];
        char commandPath1[1024];
        char commandPath1_ture[1024];
        //char commandPath1_ture_root[500] = {0};
        //char commandPath1_ture_root[500] = "/home/tony/code_book/thread_pool_book_test/server_thread/";
        char commandPath2[500];
        link_list* path_now_list;
        path_now_list = init_linklist();
       
        insert_linknode(path_now_list, 0,"/home/tony/code_book/thread_pool_book_test/server_thread");
        while(1)
        {
            //char commandPath1_ture_root[500] = "/home/tony/code_book/thread_pool_book_test/server_thread";
            char temp_path[1024] = {0};
            bzero(&commandSave,sizeof(commandSave));
            bzero(&commandPath1,sizeof(commandPath1));
            bzero(&commandPath2,sizeof(commandPath2));
            bzero(&train,sizeof(train));
            bzero(&commandPath1_ture,sizeof(commandPath1_ture));
            //bzero(&commandPath1_ture_root,sizeof(commandPath1_ture_root));
            int ret = recvn(netfd,&train.length,sizeof(train.length));
            if(ret==clientclose)
            {
                //continue;
                break;
            }
            ret = recvn(netfd,train.data,train.length);
            //printf("train.length = %d\n",train.length);
            //printf("train.data = %s\n",train.data);
            // 命令切割： 命令 参数1 参数2 
            commandDivid(train.data, commandSave,commandPath1,commandPath2);
            //虚拟地拼为绝对地址
            //strcat(commandPath1_ture_root,commandPath1);
            //strcat(commandPath1_ture,commandPath1_ture_root);
            //printf("commandPath1_ture_root = %s\n",commandPath1_ture_root);
            commandSelect(commandSave, temp_path,commandPath1,"/home/tony/code_book/thread_pool_book_test/server_thread",
            netfd,"char* fileNameToUser",path_now_list);
            
        }

        
    //登录验证 ：//第一个小火车 接收命令行字符 “password 123456" ,flag标记，成功则下次循环跳过

        //成功则继续往下；失败geto end，再次输入命令

        //断点续传(位置待定：模式一；模式二)

        //while(1)//netfd
        //{
            //启动工作路径维持:（虚拟地址转物理地址）  cd命令默认开启路径维持 并发送client1文件夹的内容(ls) 当前路径path
            //初始化链表地址

            //第一个小火车 接收命令行字符 “cd ./dir1/dir2" 

            // 命令切割： 命令 参数1 参数2 

            // 如果是cd dir1 命令，首先判断路径是否合法
                    //合法 马上发送并打印 dir1里的文件
                    //不和法 通知客户端

            // 如果不是cd命令，进入命令识别；

                //命令识别，转接接口；传递的参数：命令 参数1 参数2 
                
                    //ls接口
                    //puts接口
                    //gets接口
                    //remove接口
                    //close 客户端关闭
            
                //
        //}

        //end:
        //close(netfd);
    }
}
int makeWorker(threadPool_t *pthreadPool){
    for(int i = 0; i < pthreadPool->workerNum; ++i){
        pthread_create(&pthreadPool->tidArr[i],NULL,threadFunc,pthreadPool);

    }
    return 0;
}